package ${packageStr};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;



import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

<#if constant??>
import ${constant};
</#if>
import ${servicePath};
import ${voPath};
import ${addDtoPath};
import ${updateDtoPath};
<#if updateStatusDTOPath??>
    <#if "${statucFlag}" =="1">
        import  ${updateStatusDTOPath};
    </#if>
</#if>
import ${queryDtoPath};
import ${entityPath};

@RestController
@RequestMapping("${url}")
public class ${controllerName}{
    @Autowired
    private ${serviceName} ${serviceNm};

    @PostMapping(value="/list/limit/{page}/{pageLimit}")
    public String listLimit(@RequestBody ${queryDTO} request,@PathVariable Integer page,@PathVariable Integer pageLimit,org.springframework.ui.Model model) {
        ${entityName} entity = convert(request);
        IPage<${entityName}> response = ${serviceNm}.page(new Page<${entityName}>(page,pageLimit),new QueryWrapper<${entityName}>(entity));
        model.addAttribute("pages",convert(response));
        return "";
    }


    @GetMapping(value ="view/{${idFiled}}")
    public String view( @PathVariable("${idFiled}") ${idType} ${idFiled},org.springframework.ui.Model model) {
        ${entityName} source = ${serviceNm}.getById(${idFiled});
         model.addAttribute("entity",convert(source));
        return  "";
    }

    @PostMapping(value="/add")
    public ${resultClass }<Object> add(@RequestBody ${addDTO} request) {
        ${entityName} entity = convert(request);
        int count = ${serviceNm}.insertSelective(entity);
        return count > 0?${resultClass }.${successMethod}(Boolean.TRUE):${resultClass }.${errorMethod}("添加数据失败，请稍后再试");
    }

    @PutMapping(value="/update")
    public ${resultClass }<Object> update(@RequestBody ${updateDTO} request) {
         ${entityName} entity = convert(request);
         boolean flag =  ${serviceNm}.updateByPrimaryKey(entity);
         return flag ? ${resultClass }.${successMethod}(Boolean.TRUE):${resultClass }.${errorMethod}("修改数据失败，请稍后再试");
    }

    <#if "${statucFlag}" =="1">
        @DeleteMapping(value="/update/status")
        public ${resultClass}<Object> updateState(@RequestBody ${updateStatusDTO} request) {
            ${entityName} entity = convert(request);
            boolean flag =  ${serviceNm}.updateByPrimaryKey(entity);
            return flag ? ${resultClass }.${successMethod}(Boolean.TRUE):${resultClass }.${errorMethod}("修改数据失败，请稍后再试");
        }
    <#else>
        @DeleteMapping(value="/del/${idFiled}")
       public ${resultClass}<Object> delById(@PathVariable("${idFiled}") ${idType} ${idFiled}) {
            boolean flag =  ${serviceNm}.deleteById(${idFiled});
            return flag ? ${resultClass }.${successMethod}(Boolean.TRUE):${resultClass }.${errorMethod}("操作数据失败，请稍后再试");
        }
    </#if>

    private ${entityName} convert(${queryDTO} source) {
       return java.util.Optional.ofNullable(source).map(sc->{
                ${entityName} target = new ${entityName}();
                BeanUtils.copyProperties(sc, target);
                return target;
       }).orElse(null);
    }

    private ${entityName} convert(${updateDTO} source) {
         return java.util.Optional.ofNullable(source).map(sc->{
             ${entityName} target = new ${entityName}();
             BeanUtils.copyProperties(sc, target);
             return target;
         }).orElse(null);
    }

     <#if "${statucFlag}" =="1">
             private ${entityName} convert(${updateStatusDTO} source) {
                 return java.util.Optional.ofNullable(source).map(sc->{
                    ${entityName} target = new ${entityName}();
                    BeanUtils.copyProperties(sc, target);
                    return target;
                 }).orElse(null);
             }
     </#if>

       private ${entityName} convert(${addDTO} source) {
          return java.util.Optional.ofNullable(source).map(sc->{
             ${entityName} target = new ${entityName}();
             BeanUtils.copyProperties(sc, target);
             return target;
          }).orElseGet(null);
       }

     private ${voName} convert(${entityName} source) {
       return java.util.Optional.ofNullable(source).map(sc->{
          ${voName} target = new ${voName}();
          BeanUtils.copyProperties(source, target);
          return target;
       }).orElseGet(null);
     }

     private List<${voName}> convert(List<${entityName}> sources) {
        return java.util.Optional.ofNullable(sources).map(scs->{
          return scs.stream().map(source->{
            return convert(source);
          }).collect(java.util.stream.Collectors.toList());
        }).orElseGet(null);
     }

      private IPage<${voName}> convert(IPage<${entityName}> sources) {
             return java.util.Optional.ofNullable(sources).map(scs->{
                  IPage<${voName}> target = new com.baomidou.mybatisplus.extension.plugins.pagination.Page();
                  BeanUtils.copyProperties(scs,target);
                  List<${voName}> voNames = convert(scs.getRecords());
                  target.setRecords(voNames);

                 return target;
             }).orElseGet(null);
      }
}